package cj.web.activity.controllers;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.utils.ImageUtils;
import cj.utils.ReadConfigProperties;
import cj.utils.ReturnUUID;
import cj.web.activity.dao.GiftDAO;
import cj.web.activity.dao.GiftSellerDAO;
import cj.web.activity.domain.Gift;
import cj.web.activity.domain.GiftSeller;
import cj.web.admin.constants.MemuEnum;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

@Path("gift")
public class GiftAdminController {
	@Autowired
	private GiftSellerDAO sellerDao;
	
	@Autowired
	private GiftDAO giftDAO;
	
	@Get("show")
	public String show(){		
		return "activity/hougift/sellerList.vm";		
	}
	
	@Get("fake")
	public String fake(){		
		return "activity/hougift/fakeList.vm";
	}
	
	@Post("list")
	public String list(GiftSeller giftSeller){
		JSONObject jo = new JSONObject();
		
		List<GiftSeller> objs = sellerDao.queryByName(giftSeller);
		int size = sellerDao.countByName(giftSeller);
		
		jo.put("data", objs);
		jo.put("draw", giftSeller.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		
		return "@" + jo.toString();
	}
	
	@Get("createPage")
	public String createPage(){
		return "activity/hougift/sellerDetail.vm";
	}
	
	@Post("create")
	public String create(GiftSeller giftSeller){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		giftSeller.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
		giftSeller.setCreateAT(DateUtils.getNowTime(DateUtils.DATE_FULL_STR));
		giftSeller.setUpdateAT(DateUtils.getNowTime(DateUtils.DATE_FULL_STR));
		
		try {
			sellerDao.create(giftSeller);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model, @Param("id")String id){
		GiftSeller giftSeller = sellerDao.queryById(id);
		
		model.add("objs",giftSeller);
		
		return "activity/hougift/sellerEdit.vm";
	}
	
	@Post("update")
	public String update(GiftSeller giftSeller){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);

		giftSeller.setUpdateAT(DateUtils.getNowTime(DateUtils.DATE_FULL_STR));
		
		try {
			sellerDao.update(giftSeller);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(@Param("id")String id){
		sellerDao.deleteById(id);
		
		return "r:show";
	}
	
	@Get("items")
	public String items(Model model, @Param("sellerId")String sellerId){
		if(sellerId != null && sellerId.length() > 0){
			GiftSeller giftSeller= sellerDao.queryById(sellerId);
			model.add("seller", giftSeller);
		}
		
		return "activity/hougift/giftList.vm";
	}
	
	@Get("fakeitems")
	public String fakeitems(Model model, @Param("sellerId")String sellerId){
		if(sellerId != null && sellerId.length() > 0){
			GiftSeller giftSeller= sellerDao.queryById(sellerId);
			model.add("seller", giftSeller);
		}
		
		return "activity/hougift/fakegiftList.vm";
	}
	
	@Post("itemls")
	public String itemls(Gift gift){
		JSONObject jo = new JSONObject();
		
		List<Map> objs = giftDAO.queryByName(gift);
		int size = giftDAO.countByName(gift);
		
		jo.put("data", objs);
		jo.put("draw", gift.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		
		return "@" + jo.toString();
	}
	
	@Get("createGiftPage")
	public String createGift(Model model, @Param("sellerId")String sellerId){
		if(sellerId != null && sellerId.length() > 0){
			model.add("sellerId", sellerId);
		}
		
		return "activity/hougift/giftDetail.vm";
	}
	
	@Get("fakecreateGiftPage")
	public String fakecreateGift(Model model, @Param("sellerId")String sellerId){
		if(sellerId != null && sellerId.length() > 0){
			model.add("sellerId", sellerId);
		}
		
		return "activity/hougift/fakeGiftDetail.vm";
	}
	
	@Post("createGift")
	public String createGift(Gift gift){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		gift.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
		gift.setCreateAT(DateUtils.getNowTime(DateUtils.DATE_FULL_STR));
		gift.setUpdateAT(DateUtils.getNowTime(DateUtils.DATE_FULL_STR));
		
		try {
			giftDAO.create(gift);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("editGift")
	public String editGift(Model model, @Param("id")String id){
		Gift gift= giftDAO.queryById(id);
		
		model.add("objs",gift);
		
		return "activity/hougift/giftEdit.vm";
	}
	
	@Get("fakeeditGift")
	public String fakeeditGift(Model model, @Param("id")String id){
		Gift gift= giftDAO.queryById(id);
		
		model.add("objs",gift);
		
		return "activity/hougift/fakeGiftEdit.vm";
	}
	
	@Post("updateGift")
	public String update(Gift gift){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);

		gift.setUpdateAT(DateUtils.getNowTime(DateUtils.DATE_FULL_STR));
		
		try {
			giftDAO.update(gift);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("deleteGiftById")
	public String deleteGiftById(@Param("id")String id){
		Gift gift = giftDAO.queryById(id);
		if(gift != null){
			giftDAO.deleteById(id);
		}
		
		return "r:items?sellerId=" + gift.getSellerId();
	}
		
	@Get("fakedeleteGiftById")
	public String fakedeleteGiftById(@Param("id")String id){
		Gift gift = giftDAO.queryById(id);
		if(gift != null){
			giftDAO.deleteById(id);
		}
		
		return "r:fakeitems?sellerId=" + gift.getSellerId();
	}
	
	@Post("upload")
	public String upload(@Param("upload") MultipartFile file,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		// 判断文件是否为空  
        if (file != null && !file.isEmpty()) {  
            try {  
                // 文件保存路径  
            	String path = ReadConfigProperties.getValue("uploadFilePath");
            	//判断文件是否存在，不存在创建文件
            	Map rep = ReturnUUID.getFolder(path);
            	path = rep.get("allpath").toString();//所在的全路径
            	//文件名
                String fileName =  ReturnUUID.getName(file.getOriginalFilename()); 
                // 转存文件  
                file.transferTo(new File(path+"/"+fileName));  
                
                jo.put("filePath", rep.get("filepath")+"/"+fileName);
                
                ImageUtils.scale2(path+"/"+fileName, path+"/s_"+fileName, 180, 180, true);//缩率图
                
            } catch (Exception e) {  
                e.printStackTrace();  
                jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
            }  
        } 
		
		return "@"+jo.toString();
    }
	

}
